/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.flickr.api;

import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;

/**
 * Interface for all flickr responses so that generic 
 * error codes can be used across all requests.
 * @author craig
 *
 */
public interface FlickrResponse {

	/**
	 * Enumeration for the flickr error codes including. 
	 * <br/><b>ARGUMENTS_MISSING</b> The flickr service returns this error code if arguments are missing 
	 * from the REST url
	 * <br/><b>FORMAT_NOT_FOUND</b> The flickr service returns this error code if the service cannot 
	 * use the supplied format.
	 * <br/><b>INVALID_API_KEY</b> The flickr service returns this error code if the api key is invalid
	 * <br/><b>INVALID_SIGNATURE</b> The flickr service returns this error code if the REST url contains wrong 
	 * keys
	 * <br/><b>METHOD_NOT_FOUND</b> The flickr service returns this error code if the service cannot 
	 * find the method to initiate.
	 * <br/><b>SERVICE_UNAVAILABLE</b> The flickr service returns this error code if the service is 
	 * temporarily unavilable
	 * <br/><b>USER_NOT_FOUND</b> The flickr service returns this error code if a user is not found
	 * <br/><b>UNKNOWN</b> This is the default response until one is known
	 * <br/><b>SUCCESS</b> This is the response returned if the request was a success, 
	 * <i>this does not mean there will be data but only that the service did not error</i>
	 * @author craig
	 *
	 */
	public enum FlickrResponseErrorCode {
		USER_NOT_FOUND (),
		ARGUMENTS_MISSING(),
		INVALID_API_KEY(),
		INVALID_SIGNATURE(),
		SERVICE_UNAVAILABLE(),
		FORMAT_NOT_FOUND(),
		METHOD_NOT_FOUND(),
		SUCCESS (),
		UNKNOWN ();
		
		private static Log log = LogFactory.createInstance(FlickrResponseErrorCode.class);
		
		/**
		 * Getter of the service code number for this enumerated value
		 * @return an integer which references a specific error
		 */
		public int getCode() {
			switch(this) {
			case ARGUMENTS_MISSING: return 2;
			case FORMAT_NOT_FOUND: return 111;
			case INVALID_API_KEY: return 100;
			case INVALID_SIGNATURE: return 96;
			case METHOD_NOT_FOUND: return 112;
			case SERVICE_UNAVAILABLE: return 105;
			case USER_NOT_FOUND: return 1;
			case UNKNOWN: return -1;
			case SUCCESS: return 0;
			}
			log.error("Could not assert the value of the enumerated type " + this);
			throw new AssertionError("Could not assert the value of the enumerated type " + this);
		}
		
		/**
		 * Method to attempt to parse and find a matching error code in the enumerated flickr
		 * error code object. If found the response code is set.
		 * @param errorCode the error code found in the error tag
		 */
		public static FlickrResponseErrorCode parseCode(String code) {
			if(code != null) {
				int errCode = Integer.parseInt(code);
				for(FlickrResponseErrorCode err : FlickrResponseErrorCode.values()) {
					if(errCode == err.getCode()) {
						log.debug("Parser found error code " + err);
						return err;
					}
				}
			} else {
				log.error("Error parsing, the error code was null");
			}
			log.warn("Parser did not find the error code");
			return FlickrResponseErrorCode.UNKNOWN;
		}
		
	}
	
	/**
	 * Get the flickr response code from the request, 
	 * by default this returns UNKNOWN
	 * @return the response code enumeration for the response
	 */
	public FlickrResponseErrorCode getResponseCode();
	
}